from django.http import HttpResponse
from product.models import Product
from django.shortcuts import get_object_or_404
from django.utils import timezone
from datetime import timedelta

# 查询数据
"""
查询功能
单个查询 .get()
多个查询 .all()

过滤方法 .filter()
"""
def get_query_view(request):
    # product = Product.objects.get(pk=1)
    # 查询单个数据，不存在则返回404
    # 指定模型类，指定查询条件
    product = get_object_or_404(Product,pk=1)
    # try:
    #     product_name = Product.objects.get(product_name="小米12Pro")
    #     print(product_name)
    # except Product.DoesNotExist:
    #     return HttpResponse("商品不存在,请重新查询")      # 多数情况下，用这个
    
    return HttpResponse(f"{product.product_name}--{product.price}--{product.stock}--{product.popularity}--{product.is_active}")

def query_all_view(request):
    product = Product.objects.all()
    # print(product)
    # 查询总数量
    count = Product.objects.count()
    # print(count)
    return HttpResponse(f"{product}")

# 过滤查询使用 .filter(), .exclude(), 和 .get()
# 过滤 ==指定条件
def query_filter_view(request):
    # 相等查询，使用exact进行精确匹配
    price = Product.objects.filter(price__exact=3009)
    # print(price)
    
    # 模糊查询，使用icontains进行模糊匹配
    # 英文区分大小写
    name = Product.objects.filter(product_name__contains='p')
    # print(name)
    # icontains不区分大小写
    product_name = Product.objects.filter(product_name__icontains='p')
    # print(product_name)
    
    # startswith,endswith 匹配开头和结尾
    start = Product.objects.filter(product_name__startswith='小')
    # print(start)
    end = Product.objects.filter(product_name__endswith='清')
    # print(end)
    
    # 检查数据字段是否为空
    blank = Product.objects.filter(cover__isnull=True)
    # print(blank)
    
    # 查询价格在查询范围内的商品，range 查询范围
    price = Product.objects.filter(price__range=[999,5001])
    # print(price)
    
    # 查询价格在查询范围内的商品，in 查询范围
    price = Product.objects.filter(price__in=[1000,5000])
    # print(price)
    
    # 使用 gt, gte, lt, lte 进行大小比较
    price = Product.objects.filter(price__gt=1000)
    # print(price)
    
    price = Product.objects.filter(price__gte=1000)
    # print(price)
    
    popularity = Product.objects.filter(popularity__lt=500)
    # print(popularity)
    
    year = Product.objects.filter(created_time__startswith='2025')   # 更快
    year = Product.objects.filter(created_time__year=2025)
    # print(year)
    month = Product.objects.filter(created_time__startswith=5)
    # print(month)
    day = Product.objects.filter(created_time__startswith='2025-05-12')
    # print(day)
    datetimes = Product.objects.filter(created_time__year=2025,created_time__month=5,created_time__day=12)
    # print(datetimes)
    
    # 查询今天更新的商品
    today = Product.objects.filter(created_time__date=timezone.now().date())
    # print(today)
    # print(timezone.now().date())
    
    # 查询昨天更新的商品
    yesterday = Product.objects.filter(created_time__date=timezone.now().date() - timedelta(days=1))
    # print(yesterday)
    
    # 查询最近7天更新的商品
    seven_days = Product.objects.filter(created_time__date__range=[timezone.now().date() - timedelta(days=7),timezone.now().date()])  
    # print(seven_days)
    
    # 查询最近30天更新的商品
    thirty_days = Product.objects.filter(created_time__date__range=[timezone.now().date() - timedelta(days=30),timezone.now().date()])
    # print(thirty_days)
    
    # exclude 排队符合条件的数据
    products = Product.objects.exclude(product_name__startswith='小米').order_by('-price')[:1]
    print(products)
    
    return HttpResponse(f"{price}")
    # <QuerySet [<Product: 商品名:雷神27英寸4k超清--状态:True>, <Product: 商品名:a2奶粉 澳洲白金版 较大婴儿配方牛奶粉--状态:True>]>
    # QuerySet 并不是纯粹的python列表，而是一个django的特殊数据库查询对象

